package cn.edu.nit.sys.mapper;

import cn.edu.nit.sys.entity.Study;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @Author 罗文斌
 * @Date 2023/5/6
 * @Version 1.0.0
 */
@Mapper
public interface StudyMapper extends BaseMapper<Study> {

    Page<Study> getStudyList(Page<Study> page, String siftName);

    @Select("select id, name, volume, start_time, end_time from sys_study where id = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "id", property = "records", many = @Many(select = "getStudyLog"))
    })
    Study getDetail(String id);

    @Select("select id, seat, " +
            "if(end_time < now(), true, false) as isEmpty " +
            "from sys_study_log where study_id = #{id}")
    List<Map<String, Object>> getStudyLog(String id);

    @Insert("insert into sys_study_log values (#{id}, #{sid}, #{uid}, #{seat}, #{startTime}, #{endTime}, now())")
    int addStudyLog(String id, String sid, String uid, String seat, String startTime, String endTime);

    @Select("select id from sys_study_log where user_id = #{uid} and end_time > now()")
    String isExist(String uid);

    @Select("select a.id, a.seat, date_format(a.start_time, '%Y-%m-%d') as start, " +
            "date_format(a.end_time, '%Y-%m-%d') as end, b.name " +
            "from sys_study_log a " +
            "left join sys_study b on b.id = a.study_id " +
            "where a.user_id = #{uid}")
    Page<Map<String, String>> getLogList(Page<Map<String, String>> page, String uid);
}
